<?php
    $name = $_GET["name"];
    $a = mysqli_connect("localhost","root","gala123456");
    $db = mysqli_select_db($a,"sheep");
    $sql = "SELECT * FROM `business` WHERE `business_address` = '$name'";
    $query = mysqli_query($a,$sql);
    $address = $name;
    $num_arr = array();
    $url = array();
    $sql1 = "SELECT `business_sheepID` FROM `business` WHERE `business_address` = '$address'";
    $query1 = mysqli_query($a,$sql1);
    $num = 0;
    $fan = "no";
    while ($row1 = mysqli_fetch_row($query1)) {
        foreach ($row1 as $nei1) {
            $new_nei = explode("-",$nei1);
            for ($j = 0;$j < count($new_nei);$j++) {
                $sql2 = "SELECT `sheep_select` FROM `sheep` WHERE `sheepID` = '".$new_nei[$j]."'";
                $query2 = mysqli_query($a,$sql2);
                while ($row2 = mysqli_fetch_row($query2)) {
                    foreach ($row2 as $nei2) {
                        if ($nei2 != null) {
                            $num++;
                        }
                    }
                }
            }
        }
    }
    array_push($num_arr,$num);
    $sql3 = "SELECT `address_img` FROM `address_img` WHERE `name` = '$address'";
    $query3 = mysqli_query($a,$sql3);
    while ($row3 = mysqli_fetch_row($query3)) {
        foreach ($row3 as $nei3) {
            array_push($url,$nei3);
            $fan = "yes";
        }
    }
    $arr = array(
        "fan" => $fan,
        "address" => $address,
        "num_select" => $num_arr,
        "address_url" => $url
    );
    echo json_encode($arr);
?>